import java.util.Scanner;

class Person{
    public String name;
    public int score;
    public Person(String name,int score){
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }
}

public class TextDemo {
    public static void upSort(Person[] person){
        for(int i = 1;i< person.length;i++){
            for(int j = i;j>=1&&person[j-1].getScore()<person[j].getScore();j--){
                Person tem = person[j];
                person[j] = person[j-1];
                person[j-1] = tem;
            }
        }

    }

    public static void downSort(Person[] person){
        for(int i = 1;i< person.length;i++){
            for(int j = i;j>=1&&person[j-1].getScore()>person[j].getScore();j--){
                Person tem = person[j];
                person[j] = person[j-1];
                person[j-1] = tem;
            }
        }
    }

    public static void PrintPerson(Person[] person){
        for(int i = 0;i< person.length;i++){
            System.out.println(person[i].getName()+" "+person[i].getScore());
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextInt()) {
            int num = scanner.nextInt();
            int choice = scanner.nextInt();
            Person[] person = new Person[num];
            for (int i = 0; i < num; i++) {
                String str = scanner.next();
                int score = scanner.nextInt();
                person[i] = new Person(str, score);
            }

            if (choice == 0) {
                upSort(person);
            } else {
                downSort(person);
            }
            PrintPerson(person);
        }

    }

}
